<template>
	<view class="container">
		<view class="search-bar">
			<uni-search-bar v-model='searchVal' :focus="false" placeholder="查找教练" />
		</view>
		<view class="section-title">教练列表</view>
		<view class="block">
			<view v-for="(item, index) in trainerList" :key="index" class="card" @click="toDetail(item)">
				<image class="jiaolian" :src="addBase(item.avatar)"></image>
				<view class="info">
					<p class="name">姓名：{{ item.username }}</p>
					<p>身高：{{ item.height }}cm</p>
					<p>体重：{{ item.weight }}kg</p>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import {
	addBase
} from '../../utils/myFunction';
import request from '../../utils/request';
export default {
	data() {
		return {
			searchVal: null,
			trainerList: []
		};
	},
	onLoad(data) {
		// 如果index页面传入了参数，就不需要初始化
		if (!data) {
			request({
				url: 'system/usr/list',
				method: 'GET',
				data: {
					username: null,
					role: '教练'
				}
			}).then(res => {
				this.trainerList = res.rows
			})
		} else {
			this.searchVal = data.searchVal
		}
	},
	watch: {
		// ruoyi并未处理undefined数据
		// 会被微信小程序识别为string类型传递
		searchVal() {
			if (this.searchVal) {
				request({
					url: 'system/usr/list',
					method: 'GET',
					data: {
						username: this.searchVal,
						role: '教练'
					}
				}).then(res => {
					this.trainerList = res.rows
				})
			} else {
				request({
					url: 'system/usr/list',
					method: 'GET',
					data: {
						role: '教练'
					}
				}).then(res => {
					this.trainerList = res.rows
				})
			}
		}
	},
	methods: {
		toDetail(item) {
			uni.navigateTo({
				url: `/pages/index/detail?id=${item.id}`
			})
		},
		addBase(path) {
			return addBase(path)
		},
	}
}
</script>

<style lang="scss">
.container {
	max-width: 100%;
	padding: 0 16px;
	background-color: #f9f9f9;
}

.search-bar {
	margin-bottom: 16px;
}

.section-title {
	font-size: 18px;
	font-weight: bold;
	color: #333;
	margin: 16px 0;
	border-bottom: 2px solid #eee;
	padding-bottom: 8px;
}

.block {
	display: flex;
	flex-wrap: wrap;
	justify-content: space-between;

	.card {
		width: calc(50% - 8px);
		background-color: #fff;
		border-radius: 8px;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
		margin-bottom: 16px;
		overflow: hidden;
		transition: transform 0.3s ease;
	}

	.card:hover {
		transform: translateY(-4px);
	}

	.jiaolian {
		width: 100%;
		height: 150px;
		object-fit: cover;
	}

	.info {
		padding: 8px;

		.name {
			font-weight: bold;
			margin-bottom: 4px;
		}
	}
}
</style>